package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.mapper.QuestionsRecordsMapper;
import com.itheima.mapper.UserMapper;
import com.itheima.pojo.QuestionsRecords;
import com.itheima.pojo.User;
import com.itheima.service.QuestionsRecordsService;
import com.itheima.vo.records.QuestionsRecordsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class QuestionsRecordsServiceImpl implements QuestionsRecordsService {
    @Autowired
    private QuestionsRecordsMapper questionsRecordsMapper;
    @Autowired
    private UserMapper userMapper;
    @Override
    public List<QuestionsRecordsVO> findById(Integer id) {
        // 根据问题ID查询该问题出题记录对象集合
        LambdaQueryWrapper<QuestionsRecords> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(QuestionsRecords::getQuestionsID, id);
        List<QuestionsRecords> recordsList = questionsRecordsMapper.selectList(wrapper);
        if (recordsList == null) {
            return null;
        }
        // 根据出题记录集合,填充对应的VO集合
        List<QuestionsRecordsVO> recordsVOS = new ArrayList<>();
        for (QuestionsRecords questionsRecords : recordsList) {
            QuestionsRecordsVO questionsRecordsVO = new QuestionsRecordsVO();
            questionsRecordsVO.setOperation(questionsRecords.getOperation());
            questionsRecordsVO.setSetTime(questionsRecords.getSetTime()+"");
            User user = userMapper.selectById(questionsRecords.getSetterID());
            questionsRecordsVO.setSetter(user.getUsername());
            recordsVOS.add(questionsRecordsVO);
        }
        return recordsVOS;
    }
}
